using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.DataService;
using Prodma.DataAccess.DataService.ListService;
using Prodma.DataAccess.Helper.Enumerators;
using Prodma.DataAccess.Authorization;
using Prodma.DataAccess.Repositories;

namespace Prodma.Panel.Controllers
{
    [Authorize]
    public class MenuController : Controller
    {

        #region Repository Setters

        private IMenuRepository repository;

        public MenuController() : this(new MenuService()) { }

        public MenuController(IMenuRepository _repository)
        {
            repository = _repository;
        }

        #endregion


        #region Index
        [NavigationFilter(MenuAdmins.MenuManagement, AuthorizationType.Read)]
        public ActionResult Index()
        {
            int location = Convert.ToInt32(Request.QueryString["location"]);
            return View(repository.GetList(location));
        }
        #endregion


        #region New
        [NavigationFilter(MenuAdmins.MenuManagement, AuthorizationType.Write)]
        public ActionResult New()
        {
            int location = Convert.ToInt32(Request.QueryString["location"]);

            ViewData["parentMenuList"] = ParentMenuList.GetList(location);
            ViewData["stateList"] = StateList.GetList(StateType.General);
            ViewData["menuLocationList"] = MenuLocationList.GetList();
            ViewData["targetList"] = MenuTargetList.GetList();

            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [NavigationFilter(MenuAdmins.MenuManagement, AuthorizationType.Write)]
        public ActionResult New(FormCollection form)
        {
            ViewData["stateList"] = StateList.GetList(StateType.General);
            ViewData["menuLocationList"] = MenuLocationList.GetList();
            ViewData["targetList"] = MenuTargetList.GetList();

            tb_Menu menu = new tb_Menu();
            if (Request.Form["ddlParentMenu"] != "" || Request.Form["ddlParentMenu"] != null)
            {
                menu.ParentId = Convert.ToInt32(Request.Form["ddlParentMenu"]);
            }
            else
            {
                menu.ParentId = 0;
            }
            menu.tb_MenuLocationReference.EntityKey = new EntityKey("DataContext.tb_MenuLocation", "Id", Convert.ToInt32(Request.Form["ddlMenuLocation"]));
            menu.Name = Request.Form["name"];
            menu.tb_MenuTypeReference.EntityKey = new EntityKey("DataContext.tb_MenuType", "Id", Convert.ToInt32(Request.Form["menuType"]));
            int menuType = Convert.ToInt32(Request.Form["menuType"]);
            if (menuType == MenuType.Category.ToInt())
            {
                menu.Item = Convert.ToInt32(Request.Form["itemId"]);
                menu.Target = 0;
            }
            else if (menuType == MenuType.Content.ToInt())
            {
                menu.Item = Convert.ToInt32(Request.Form["itemId"]);
                menu.Target = 0;
            }
            else
            {
                menu.Item = 0;
                if (Request.Form["ddlTarget"] != "")
                {
                    menu.Target = Convert.ToInt32(Request.Form["ddlTarget"]);
                }
                else
                {
                    menu.Target = 0;
                }
            }
            menu.Url = Request.Form["url"];
            menu.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(Request.Form["ddlState"]));
            menu.tb_MenuLocationReference.EntityKey = new EntityKey("DataContext.tb_MenuLocation", "Id", Convert.ToInt32(Request.Form["ddlMenuLocation"]));

            repository.New(menu);

            int location = Convert.ToInt32(Request.Form["location"]);
            return RedirectToAction("new/?location=" + location);
        }
        #endregion


        #region Edit
        [NavigationFilter(MenuAdmins.MenuManagement, AuthorizationType.Update)]
        public ActionResult Edit()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            MenuVm menu = repository.Edit(id);

            int location = Convert.ToInt32(Request.QueryString["location"]);
            ViewData["parentMenuList"] = ParentMenuList.GetList(location, menu.Parent.Id);
            if (menu.Type.Id == MenuType.Content.ToInt())
            {
                ViewData["contentNameData"] = ContentList.GetContentName(menu.Item.Value);
            }
            else if (menu.Type.Id == MenuType.Category.ToInt())
            {
                ViewData["categoryNameData"] = CategoryList.GetCategoryName(menu.Item.Value);
            }
            else
            {
                ViewData["urlData"] = menu.Url;
            }
            ViewData["targetList"] = MenuTargetList.GetList(menu.Target);
            ViewData["stateList"] = StateList.GetList(StateType.General, menu.State.Id);

            return View(menu);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [NavigationFilter(MenuAdmins.MenuManagement, AuthorizationType.Update)]
        public ActionResult Edit(FormCollection form)
        {
            tb_Menu menu = new tb_Menu();

            menu.Name = Request.Form["name"];
            menu.tb_MenuTypeReference.EntityKey = new EntityKey("DataContext.tb_MenuType", "Id", Convert.ToInt32(Request.Form["menuType"]));
            int menuType = Convert.ToInt32(Request.Form["menuType"]);
            if (menuType == MenuType.Category.ToInt())
            {
                menu.Item = Convert.ToInt32(Request.Form["itemId"]);
                menu.Target = 0;
            }
            else if (menuType == MenuType.Content.ToInt())
            {
                menu.Item = Convert.ToInt32(Request.Form["itemId"]);
                menu.Target = 0;
            }
            else
            {
                menu.Item = 0;
                menu.Target = Convert.ToInt32(Request.Form["ddlTarget"]);
            }

            menu.Url = Request.Form["url"];
            if (Request.Form["ddlTarget"] == null)
            {
                menu.Target = 1;
            }
            else
            {
                if (Request.Form["ddlTarget"] != "")
                {
                    menu.Target = Convert.ToInt32(Request.Form["ddlTarget"]);
                }
                else
                {
                    menu.Target = 0;
                }
            }
            menu.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(Request.Form["ddlState"]));

            int id = Convert.ToInt32(Request.Form["id"]);
            repository.Update(id, menu);

            int location = Convert.ToInt32(Request.Form["location"]);
            return RedirectToAction("edit/?id=" + id + "&location=" + location);
        }
        #endregion


        #region Delete
        [NavigationFilter(MenuAdmins.MenuManagement, AuthorizationType.Delete)]
        public ActionResult Delete()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string deleteId = id.Substring(3, id.Length - 3);
                repository.Delete(Convert.ToInt32(deleteId));
            }

            return RedirectToAction("index");
        }
        #endregion


        #region Set Active && Passive
        [NavigationFilter(MenuAdmins.MenuManagement, AuthorizationType.Update)]
        public ActionResult SetActive()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string currentId = id.Substring(3, id.Length - 3);
                repository.SetActive(Convert.ToInt32(currentId));
            }

            return RedirectToAction("index");
        }
        [NavigationFilter(MenuAdmins.MenuManagement, AuthorizationType.Update)]
        public ActionResult SetPassive()
        {
            string[] idList = Request.QueryString["idList"].Split(',');

            foreach (string id in idList)
            {
                string currentId = id.Substring(3, id.Length - 3);
                repository.SetPassive(Convert.ToInt32(currentId));
            }

            return RedirectToAction("index");
        }
        #endregion


        #region Order Operation

        public ActionResult OrderUp()
        {
            int menuId = Convert.ToInt32(Request.QueryString["id"]);
            int orderNo = Convert.ToInt32(Request.QueryString["orderNo"]);
            int location = Convert.ToInt32(Request.QueryString["location"]);
            int parentId = Convert.ToInt32(Request.QueryString["parentId"]);

            MenuOrderVm menuOrder = new MenuOrderVm();
            menuOrder.MenuId = menuId;
            menuOrder.OrderNo = orderNo;
            menuOrder.Location = location;
            menuOrder.ParentId = parentId;

            repository.OrderUp(menuId, menuOrder);

            return RedirectToAction("/index/?location=" + Request.QueryString["location"]);
        }

        public ActionResult OrderDown()
        {
            int menuId = Convert.ToInt32(Request.QueryString["id"]);
            int orderNo = Convert.ToInt32(Request.QueryString["orderNo"]);
            int location = Convert.ToInt32(Request.QueryString["location"]);
            int parentId = Convert.ToInt32(Request.QueryString["parentId"]);

            MenuOrderVm menuOrder = new MenuOrderVm();
            menuOrder.MenuId = menuId;
            menuOrder.OrderNo = orderNo;
            menuOrder.Location = location;
            menuOrder.ParentId = parentId;

            repository.OrderDown(menuId, menuOrder);

            return RedirectToAction("/index/?location=" + Request.QueryString["location"]);
        }

        public ActionResult SetOrder()
        {
            int menuId = Convert.ToInt32(Request.QueryString["id"]);
            int orderNo = Convert.ToInt32(Request.QueryString["orderNo"]);
            int setOrderNo = Convert.ToInt32(Request.QueryString["setOrderNo"]);
            int location = Convert.ToInt32(Request.QueryString["location"]);
            int parentId = Convert.ToInt32(Request.QueryString["parentId"]);

            if (setOrderNo < 0)
            {
                return RedirectToAction("/index/?location=" + Request.QueryString["location"]);
            }

            MenuOrderVm menuOrder = new MenuOrderVm();
            menuOrder.MenuId = menuId;
            menuOrder.OrderNo = orderNo;
            menuOrder.Location = location;
            menuOrder.ParentId = parentId;
            menuOrder.SetOrderNo = setOrderNo;

            repository.SetOrder(menuId, menuOrder);

            return RedirectToAction("/index/?location=" + Request.QueryString["location"]);
        }
        #endregion


        #region Get Parent MenuList
        public JsonResult GetParentMenuList(int location)
        {
            return this.Json(ParentMenuList.GetList(location));
        }
        #endregion



        #region Get Content List AJAX
        public ActionResult GetContentListAJAX()
        {
            int page = 0;
            if (Request.Form["page"] != "" && Request.Form["page"] != null)
            {
                page = Convert.ToInt32(Request.Form["page"]);
            }

            ViewData["categoryList"] = ContentList.GetCategoryListForMenu();
            ViewData["contentCounts"] = ContentList.GetCountsForMenu();

            if (Request.QueryString["find"] != "" && Request.QueryString["find"] != null)
            {
                Dictionary<string, string> find = new Dictionary<string, string>();
                find.Add("name", Request.QueryString["find"]);
                ViewData["totalRowCount"] = ContentList.GetTotalRowCountForMenu(find);
                return PartialView("ContentList", ContentList.GetListForMenu(page,find));
            }
            else
            {
                ViewData["totalRowCount"] = ContentList.GetTotalRowCountForMenu();
                return PartialView("ContentList", ContentList.GetListForMenu(page));
            }
        }
        #endregion

        #region Get Category List AJAX
        public ActionResult GetCategoryListAJAX()
        {
            int page = 0;
            if (Request.Form["page"] != "" && Request.Form["page"] != null)
            {
                page = Convert.ToInt32(Request.Form["page"]);
            }

            ViewData["sectionList"] = CategoryList.GetSectionListForMenu();

            ViewData["totalRowCount"] = CategoryList.GetTotalRowCountForMenu();
            ViewData["contentCounts"] = CategoryList.GetCountsForMenu();

            return PartialView("CategoryList", CategoryList.GetListForMenu(page));
        }
        #endregion

    }
}
